package com.ssm.netty.splitpacket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class MyClientHandler extends SimpleChannelInboundHandler<MyMessageProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            String msg = "你好, 我是张三!";
            byte[] msgBytes = msg.getBytes(CharsetUtil.UTF_8);
            // 创建协议包对象
            MyMessageProtocol protocol = new MyMessageProtocol();
            protocol.setLen(msgBytes.length);
            // 我们只能发送发送二进制数据, 想发送对象的话需要自己实现编解码器
            protocol.setContent(msgBytes);
            // 发送到服务端
            ctx.writeAndFlush(protocol);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MyMessageProtocol myMessageProtocol) throws Exception {
    }
}
